package com.han.bom;

/**
 * An auxiliary class to facilitate the duration of an exam
 * 
 * @author Ric Janus Sapasap
 * @version 1.0
 * @since JDK 1.7
 * @see ExamResponse
 * @see Exam
 * @see Thread
 */
public class ExamThread extends Thread {
	
	private long duration;
	private ExamResponse response;
	
	/**
	 * Sole Constructor, initializes the duration of the Thread in seconds, and the corresponding ExamResponse Instance
	 * 
	 * @param duration length of the exam in seconds
	 * @param response the corresponding ExamResponse 
	 */
	public ExamThread(long duration, ExamResponse response){
		super();
		this.duration = duration;
		this.response = response;
	}
	
	/* (non-Javadoc)
	 * Decrements a timer, set initially to the value of the duration, until it becomes zero. Each decrement represents one real second.
	 * When the timer reaches zero, the corresponding ExamResponse is disabled and any further changes to the answers are blocked
	 * 
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run(){
		
		long timer = this.duration;
		
		while(timer > 0){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			timer--;
		}
		this.response.setActive(false);
	}
}
